package com.wiim.tab;

import android.app.Activity;
import android.app.TabActivity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TextView;
import android.widget.Toast;

import com.wiim.common.ConstantValue;
import com.wiim.common.Utilities;
import com.wiim.common.Utils;
import com.wiim.libs.AppPreferencesLib;
import com.wiim.nhapnhap.CustomFragmentActivity;
import com.wiim.nhapnhap.R;
import com.wiim.ui.QRCodeFrament;
import com.wiim.ui.TabHome;
import com.wiim.ui.TabQrCode;
import com.wiim.ui.TabDiaDiem;

/**
 * The Class TabWidget.
 */

@SuppressWarnings("deprecation")
public class TabWidget extends TabActivity implements OnClickListener {

	/** The tab host car. */
	public static TabHost tabHostCAR;
	public static TabHost tabHost;
	private RelativeLayout rlTab;
	private LinearLayout lnTab;
	private LinearLayout llBgTop;
	private TextView tvTitleApp;
	private Button btnBackNow;
	private Button btnThongtin;
	private Button btnSetup;
	private Button btnLogOut;
	private Button btnGPS;
	private Button btnTabLogin;
	private Button btn;
	private Button btnDoithuong;
	private Button btnDiadiem;

	private RelativeLayout rlLayoutTopBar;
	public static final int REQUEST_CODE_QR = 0x0000c0de;
	private AppPreferencesLib appPreferentceslib;

	float scale;
	int zoomSizeDefault = 0;

	/**
	 * Inits the listener.
	 */
	/**
	 * [Explain the description for this method here].
	 * 
	 * @param saveBundle
	 * @see android.app.ActivityGroup#onCreate(android.os.Bundle)
	 */
	@SuppressWarnings("deprecation")
	@Override
	public void onCreate(final Bundle saveBundle) {
		super.onCreate(saveBundle);
		this.setContentView(R.layout.activity_main);
		init();
	}

	TabElement tabElementWidget = null;

	@SuppressWarnings("deprecation")
	public void init() {
		Bundle bundle = this.getIntent().getExtras();
		this.tvTitleApp = (TextView) findViewById(R.id.tvTitleApp);
		btnBackNow = (Button) findViewById(R.id.btnBackNow);
		btnLogOut = (Button) findViewById(R.id.btnLogOut);
		btn = (Button) findViewById(R.id.btn);
		btnDoithuong = (Button) findViewById(R.id.btnDoithuong);
		btnDiadiem = (Button) findViewById(R.id.btnDiadiem);

		rlLayoutTopBar = (RelativeLayout) findViewById(R.id.layout_top_bar);

		lnTab = (LinearLayout) findViewById(R.id.lnTab);
		llBgTop = (LinearLayout) findViewById(R.id.llBgTop);
		btn.setOnClickListener(this);
		btnDiadiem.setOnClickListener(this);
		btnDoithuong.setOnClickListener(this);

		this.tabHost = this.getTabHost(); // The activity TabHost
		tabHostCAR = this.getTabHost();
		TabHost.TabSpec tabSpec; // Resusable TabSpec for each tab
		Intent intent; // Reusable Intent for each tab

		// Add Tab Home. Index = 0.
		intent = new Intent().setClass(this, TabHome.class);
		tabElementWidget = new TabElement(this, R.drawable.btn_qc, R.drawable.btn_qc, "", false, "Home1", false);
		tabElementWidget.setBackgroundDrawable(this.getResources().getDrawable(R.drawable.transparent));
		tabSpec = this.tabHost.newTabSpec("0").setIndicator(tabElementWidget).setContent(intent);

		this.tabHost.addTab(tabSpec);

		// Add Tab About. Index = 1.
		intent = new Intent().setClass(this, TabQrCode.class);
		tabElementWidget = new TabElement(this, R.drawable.transparent, R.drawable.transparent, "QR Code", false, "Location1", false);
		tabElementWidget.setBackgroundDrawable(this.getResources().getDrawable(R.drawable.transparent));
		tabSpec = this.tabHost.newTabSpec("1").setIndicator(tabElementWidget).setContent(intent);
		this.tabHost.addTab(tabSpec);

		// Add Tab Contact. Index = 2.
		intent = new Intent().setClass(this, TabDiaDiem.class);
		tabElementWidget = new TabElement(this, R.drawable.btn_diadiem, R.drawable.btn_diadiem, "", false, "Search1", false);
		tabElementWidget.setBackgroundDrawable(this.getResources().getDrawable(R.drawable.transparent));
		tabSpec = this.tabHost.newTabSpec("2").setIndicator(tabElementWidget).setContent(intent);
		this.tabHost.addTab(tabSpec);

		// Set Tab Index
		this.tabHost.setCurrentTab(bundle.getInt("indexTab"));
		((TabElement) tabHost.getTabWidget().getChildAt(bundle.getInt("indexTab"))).tvTabElement.setTextColor(Color.rgb(212, 118, 47));
		((TabElement) this.tabHost.getTabWidget().getChildAt(0)).changeTitle("");

		this.tabHost.setOnTabChangedListener(new OnTabChangeListener() {

			@Override
			public void onTabChanged(final String arg0) {
				int tabIndex = 0;
				for (int i = 0; i < 3; i++) {

					if (Integer.parseInt(arg0) == i) {
						tabIndex = i;
//						if (i == 2) {
//							btnDiadiem.setVisibility(View.VISIBLE);
//							btnDiadiem.setBackgroundResource(R.drawable.ic_launcher);
//							btnDoithuong.setVisibility(View.GONE);
//							btnDoithuong.setBackgroundResource(R.drawable.ic_launcher);
//						}
//						if (i == 0) {
//							btnDoithuong.setVisibility(View.VISIBLE);
//							btnDoithuong.setBackgroundResource(R.drawable.ic_launcher);
//							btnDiadiem.setVisibility(View.GONE);
//							btnDiadiem.setBackgroundResource(R.drawable.ic_launcher);
//						}
						((TabElement) tabHost.getTabWidget().getChildAt(i)).tvTabElement.setTextColor(Color.rgb(212, 118, 47));
						System.out.println("hai index: " + tabIndex);
					} else {
						((TabElement) tabHost.getTabWidget().getChildAt(i)).tvTabElement.setTextColor(Color.WHITE);
						System.out.println("hai index none: " + i);
					}
				}
			}
		});
	}

	@Override
	public void onWindowFocusChanged(boolean hasFocus) {
		// TODO Auto-generated method stub
		super.onWindowFocusChanged(hasFocus);
	}

	/**
	 * Switch tab.
	 * 
	 * @param context
	 *            the context
	 * @param index
	 *            the index
	 */
	public void switchTab(final int index) {

		this.tabHost.setup();
		this.tabHost.setCurrentTab(index);
	}

	public void initTabTitle() {

		((TabElement) this.tabHost.getTabWidget().getChildAt(0)).changeTitle("home");

		((TabElement) this.tabHost.getTabWidget().getChildAt(1)).changeTitle("location");

		((TabElement) this.tabHost.getTabWidget().getChildAt(2)).changeTitle("searrcb");

	}

	@Override
	public void onClick(View v) {
		CustomFragmentActivity cus = new CustomFragmentActivity();
		if (v == btn) {

			TurnWebcameOn();
		}
		if (v == btnDiadiem) {

		}
		if (v == btnDoithuong) {
			TabWidget.tabHost.setCurrentTab(0);
			// TabHome.getInstance().a();
		}

	}

	public void TurnWebcameOn() {
		Utilities.getGlobalVariable(TabWidget.this).isShowed = true;
		Intent intent = new Intent("com.google.zxing.client.android.SCANQR");
		intent.setPackage("com.wiim.nhapnhap");
		intent.putExtra("SCAN_MODE", "QR_CODE_MODE");
		startActivityForResult(intent, REQUEST_CODE_QR);

	}

	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent intent) {
		if (requestCode == REQUEST_CODE_QR) {
			if (resultCode == Activity.RESULT_OK) {
				String tagCode = intent.getStringExtra("SCAN_RESULT").trim();
				Toast.makeText(TabWidget.this, tagCode, Toast.LENGTH_SHORT).show();
			}
		} else {
			Toast.makeText(TabWidget.this, "cancel", Toast.LENGTH_SHORT).show();
		}

	}

}
